package com.qdxwx.manage.controller.company;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.company.CompanyService;
import com.qdxwx.manage.annotation.Log;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.company.Company;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 基本设置Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/company")
public class CompanyConfigController extends BaseController {

    @Autowired
    private CompanyService companyService;

    @GetMapping("/config.html")
    public String edit(ModelMap mmap) {
        Manager manager = SessionUtils.getManager();
        if(manager == null) return error500;
        Company company = companyService.get(manager.getCompanyId());
        if(company == null) return error404;
        mmap.put("company", company);
        return "company/company-config";
    }

    /**
     * 修改保存基本设置
     */
    @Log(title = "保存基本设置", businessType = BusinessType.UPDATE)
    @Permissions("company:config")
    @PostMapping("/config.json")
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if (manager == null) return AjaxResult.error("请登录后操作！");
        String companyName = TKString.queryKey(request.getParameter("companyName"));
        if (TKString.isEmpty(companyName)) return error("机构名称不能为空");
        String siteName = TKString.queryKey(request.getParameter("siteName"));
        if (TKString.isEmpty(siteName)) return error("网站名称不能为空");
        String siteDomain = TKString.trim(request.getParameter("siteDomain"));
        String description = TKString.trim(request.getParameter("description"));
        String keywords = TKString.trim(request.getParameter("keywords"));
        String copyright = TKString.trim(request.getParameter("copyright"));
        String contact = TKString.trim(request.getParameter("contact"));
        int companyId = TKConvert.toInteger(request.getParameter("companyId"));
        if (manager.getCompanyId() != companyId) {
            return error("非法操作！");
        }
        Company company = companyService.get(companyId);
        if (company == null) {
            return error("找不到指定修改的机构");
        }
        company.setCompanyName(companyName);
        company.setSiteName(siteName);
        company.setSiteDomain(siteDomain);
        company.setLogo(TKString.trim(request.getParameter("logo")));
        company.setIco(TKString.trim(request.getParameter("ico")));
        company.setQrcode(TKString.trim(request.getParameter("qrcode")));
        company.setDescription(description);
        company.setKeywords(keywords);
        company.setCopyright(copyright);
        company.setContact(contact);
        company.setUpdateBy(manager.getManagerName());
        return toAjax(companyService.update(company));
    }
}
